package com.wkq.service.impl;

import com.github.pagehelper.PageHelper;
import com.wkq.dao.IMemberDao;
import com.wkq.dao.IOrdersDao;
import com.wkq.dao.IProductDao;
import com.wkq.dao.ITravellerDao;
import com.wkq.domain.Orders;
import com.wkq.domain.Product;
import com.wkq.domain.Traveller;
import com.wkq.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class IOrdersServiceImpl implements IOrdersService {

    @Autowired
    private IOrdersDao iOrdersDao;
    @Autowired
    private IProductDao iProductDao;
    @Autowired
    private IMemberDao iMemberDao;
    @Autowired
    private ITravellerDao iTravellerDao;

    //根据订单ID查询相应的订单数据详情
    @Override
    public Orders findById(int id) {
        Orders orders = iOrdersDao.findById(id);
        Product product = iProductDao.findById(orders.getProductId());
        orders.setProduct(product);
        if (orders.getMemberId() > 0) {
            orders.setMember(iMemberDao.findById(orders.getMemberId()));
        }
        List<Traveller> TravellerList = iTravellerDao.findAll(Integer.parseInt(orders.getId()));
        orders.setTravellers(TravellerList);
        return orders;
    }

    //分页查询订单数据
    @Override
    public List<Orders> findAll(String page, String size) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        List<Orders> list = iOrdersDao.findAll(Integer.parseInt(page), Integer.parseInt(size));
        Iterator<Orders> iterator = list.iterator();
        while (iterator.hasNext()) {
            Orders orders = iterator.next();
            Product product = iProductDao.findById(orders.getProductId());
            orders.setProduct(product);
        }
        return list;
    }
}
